#include <iconButton.h>

namespace View {

  gboolean
  on_icon_press_event_cb (GooCanvasItem *item,
			  GooCanvasItem *target_item,
			  GdkEventButton *event,
			  gpointer user_data)
  {
    IconButton *iconButton =(IconButton*)user_data;
    if(false == iconButton->frozen)
      iconButton->OnClicked();
    return true;
  }
  
  gboolean
  on_motion_notify_event_cb (GooCanvasItem  *item,
			     GooCanvasItem  *target_item,
			     GdkEventMotion *event,
			     gpointer user_data)
  {
    IconButton *iconButton =(IconButton*)user_data;
    return iconButton->SetState(ICON_STATE_MG_ONSHOW);
  }
  
  
  gboolean
  on_leave_notify_event_cb (GooCanvasItem  *item,
			    GooCanvasItem  *target_item,
			    GdkEventMotion *event,
			    gpointer user_data)
  {
    IconButton *iconButton =(IconButton*)user_data;
    return iconButton->SetState(ICON_STATE_BG_ONSHOW);
  }
  
  IconButton::IconButton(GooCanvasItem * newParent,gdouble x,gdouble y,GdkPixbuf *bg,GdkPixbuf *mg,GdkPixbuf *fg)
    :coordinatesX(x),
     coordinatesY(y),
     parent(newParent),
     frozen(false),
     data(NULL)
  {
    canvas = goo_canvas_group_new(parent,
				  "x",(gdouble)coordinatesX, 
				  "y",(gdouble)coordinatesY,
				  "can-focus", TRUE, NULL
				  );
    normalBackground = goo_canvas_image_new (canvas, bg, 0, 0, NULL);
    mouseOverBackground = goo_canvas_image_new (canvas, mg, 0, 0, NULL);
    fadeBackground = goo_canvas_image_new (canvas, fg, 0, 0, NULL);
    SetState(ICON_STATE_BG_ONSHOW);
    g_signal_connect (G_OBJECT (canvas), "button-press-event", 
		      G_CALLBACK (on_icon_press_event_cb), this);
    g_signal_connect (G_OBJECT (canvas), "motion-notify-event", 
		      G_CALLBACK (on_motion_notify_event_cb), this);
    g_signal_connect (G_OBJECT (canvas), "leave-notify-event", 
		      G_CALLBACK (on_leave_notify_event_cb), this);
  }
  
  IconButton::~IconButton()
  {
    if(normalBackground)
      goo_canvas_item_remove(normalBackground);
    if(mouseOverBackground)
      goo_canvas_item_remove(mouseOverBackground);
    if(fadeBackground)
      goo_canvas_item_remove(fadeBackground);
    if(canvas)
      goo_canvas_item_remove(canvas);
    
  }

  bool IconButton::SetData( void *newData) 
  {
    if(NULL == newData)
      return false;
    data = newData ;
    return true;
  }

  bool IconButton::SetState(IconState newState)
  {
    if(newState == iconState)
      return true;
    if(frozen)
      return false;

    switch(newState)
      {
      case ICON_STATE_HIDE:
	{
	  g_object_set(normalBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	  g_object_set(mouseOverBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
          g_object_set(fadeBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	}break;
      case ICON_STATE_BG_ONSHOW:
	{
	  g_object_set(normalBackground,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
	  g_object_set(mouseOverBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	  g_object_set(fadeBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	}break;
      case ICON_STATE_MG_ONSHOW:
	{
	  g_object_set(normalBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
          g_object_set(mouseOverBackground,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
          g_object_set(fadeBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	}break;
      case ICON_STATE_FG_ONSHOW:
	{
	  g_object_set(normalBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
          g_object_set(mouseOverBackground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
          g_object_set(fadeBackground,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
	}break;
      default : return false;
      }
    iconState = newState;
    return true;
  }

  bool IconButton::MoveTo(double xOffset, double yOffset)
  {
    g_object_set(canvas,
		 "x" , xOffset,
		 "y" ,  yOffset, 
		 NULL);
    return true;
  }

  bool IconButton::Hide()
  {
    //    SetState(ICON_STATE_HIDE);
    g_object_set(canvas,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    return true;
  }

  bool IconButton::Show()
  {
    //SetState(ICON_STATE_BG_ONSHOW);
    g_object_set(canvas,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    return true;
  }

  bool IconButton::Fade()
  {
    SetState(ICON_STATE_FG_ONSHOW);
    frozen = true;
    return true;
  }

  bool IconButton::UnFade()
  {
    SetState(ICON_STATE_BG_ONSHOW);
    frozen = false;
    return true;
  }

}
